/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship_server.threads;

import battleship_server.SocketConnection;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author omar
 */
public class SetShipsThread extends Thread {

    int playerNumber;
    GameLogicThread gameThread;
    SocketConnection socketConnection;

    public SetShipsThread(int pNumber, GameLogicThread glt, SocketConnection pl) {
        this.playerNumber = pNumber;
        this.gameThread = glt;
        this.socketConnection = pl;
    }

    @Override
    public void run() {
        socketConnection.setShipsThread = this;
        socketConnection.getOut().println("READY_TO_SET");
    }

    public void selectShips() {
        for (int x = 2; x < 7; x++) {
            try {
                boolean vertical = (Integer.parseInt(socketConnection.getIn().readLine()) == 1 ? true : false);
                int x1 = Integer.parseInt(socketConnection.getIn().readLine());
                int y1 = Integer.parseInt(socketConnection.getIn().readLine());
                int x2 = (vertical ? x1 : x1 + x);
                int y2 = (vertical ? y1 + x : y1);
                gameThread.setShip(playerNumber, x, x1, y1, x2, y2);
            } catch (IOException ex) {
                Logger.getLogger(SocketConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
